package com.shiyi.controller;

import com.shiyi.handle.WebSocketServerSingle;
import com.shiyi.utils.IpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * websocket 接口服务
 */
@RestController
@Slf4j
public class ApiSocketController {

    // 推送数据到websocket客户端 接口
    @GetMapping("/socket/push")
    public Map pushMessage(String ip) {
        Map<String, Object> result = new HashMap<>();
//        String ip = IpUtil.getIp();
        String ip2region = IpUtil.getIp2region(ip);
        try {
            String msg = "服务端推送消息：ip= " + ip + ",地址= " + ip2region;
            log.error(msg);
            WebSocketServerSingle.sendMessages(msg);
            result.put("msg", msg);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

}
